const app = getApp();
import { fans_list,followUser_opt } from "../../../api/my"
import { getStorage } from "../../../utils/localStorage"
import { baseUrl } from "../../../api/release"
Page({
    data: {
        navData: {
            level: 2,
            title: "粉丝"
        },
        user: getStorage('user'),
        fensList: [], //列表数据
        imgUrl: baseUrl,
        navMargin: app.globalData.navHeight + app.globalData.navTop,
    },
    onLoad(options) {
        this.setData({
            user: getStorage('user'),
            imgUrl: baseUrl,
        });
        this.getFensList();
    },
    onReady() {

    },
    onShow() {
        this.getFensList();
    },
    //获取关注的用户列表
    getFensList() {
        fans_list({ userId: this.data.user.id }).then(res => {
            this.setData({
                fensList: res.data.data
            });
        });
    },
    //列表点击事件
    listClick(e) {
        let item = e.currentTarget.dataset.item;
        wx.navigateTo({
            url: '/pagesWoDe/pages/personalHomepage/index?item=' + encodeURIComponent(JSON.stringify(item)),
        })
    },
    //关注按钮事件
    followOpt(e) {
        let item = e.currentTarget.dataset.item,
            index = e.currentTarget.dataset.index,
            state = item.state ? false : true;
        if (state) {
            this.getFollow(index, item.id, state);

        } else {
            wx.showModal({
                title: '用户关注',
                content: '请确定是否要取消对该用户的关注',
                complete: (res) => {
                    if (res.confirm) {
                        this.getFollow(index, item.id, state);
                    };
                }
            });
        };

    },
    //关注操作请求
    getFollow(index, id, state) {
        followUser_opt({ id: this.data.user.id, userId: id, state: state }).then(res => {
            if (res.data.code === 200) {
                wx.showToast({
                    icon: 'none',
                    title: res.data.msg,
                    duration: 1000,
                });
                let value = "fensList[" + index + "].state";
                this.setData({
                    [value]: state
                });
            };
        });
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})